package com.stu.ui;

import com.stu.beans.Student;
import com.stu.beans.User;
import com.stu.control.StuControl;
import com.stu.control.UserControl;
import com.stu.service.StuService;
import com.stu.utils.MD5Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 命令行模拟前端
 */
public class Ui {
    private UserControl userControl = new UserControl();
    private StuControl stuControl = new StuControl();
    public void go(){
        System.out.println("1.注册用户  2.用户登录  ");
        switchUI();
    }

    private void switchUI(){
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        switch (s){
            case "1":
                registerUser();
                break;
            case "2":
                login();
                break;
            default:
                System.out.println("输入有误，请重新输入");
                switchUI();
                break;
        }
    }

    /**
     * 查询学生信息
     */
    private void queryStudentInfo() {
        System.out.println("请选择查询的条件 1.根据学号查询 2.根据姓名查询 3.根据手机号查询");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        Student student = new Student();
        List<Student> sruData = new ArrayList<>();
        switch (s){
            case "1":
                System.out.println("请输入学号");
                s = scanner.nextLine();
                student.setSno(s);
                sruData.addAll(stuControl.queryStu(student));
                break;
            case "2":
                System.out.println("请输入学生姓名");
                s = scanner.nextLine();
                student.setName(s);
                sruData.addAll(stuControl.queryStu(student));
                break;
            case "3":
                System.out.println("请输入学生手机号");
                s = scanner.nextLine();
                student.setPhone(s);
                sruData.addAll(stuControl.queryStu(student));
                break;
            default:
                queryStudentInfo();
                break;
        }
        System.out.println("查询到的学生是");
        for(Student item : sruData){
            System.out.println("--->" + item.toString());
        }
        changeStudent();
    }

    /**
     * 新增学生信息
     */
    private void newStudentInfo() {
        System.out.println("请输入学生信息，学号 姓名 年龄 性别 手机号；例如：1 张三 20 男 15641526352");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] split = s.split(" ", 5);
        String sno = split[0];
        String name = split[1];
        int age = Integer.parseInt(split[2]);
        String gender = split[3];
        String phone = split[4];
        boolean b = stuControl.addStu(new Student(sno, name, age, gender, phone));
        if (b) {
            System.out.println("添加学生成功");
            changeStudent();
        } else {
            System.out.println("添加学生失败， 请重新添加");
            newStudentInfo();
        }
    }

    /**
     * 用户登录
     */
    private void login() {
        System.out.println("请输入用户名和密码，用空格隔开 例如：张三 123456");
        Scanner scanner = new Scanner(System.in);
        String userInfo = scanner.nextLine();
        String[] split = userInfo.split(" ", 2);
        String name = split[0];
        String psd = split[1];
        // 对密码加密
        psd = MD5Util.md5EnCode(psd);
        boolean login = userControl.login(new User(name, psd));
        if (login){
            System.out.println("登录成功");
            changeStudent();
        }else {
            System.out.println("登录失败，请重新登录");
            login();
        }
    }

    private void changeStudent(){
        System.out.println("3.录入学生信息 4.查询学生信息 5.修改学生信息");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        switch (s) {
            case "3":
                newStudentInfo();
                break;
            case "4":
                queryStudentInfo();
                break;
            case "5":
                modifyStudentInfo();
                break;
            default:
                System.out.println("输入有误，请重新输入");
                break;
        }
    }

    /**
     * 修改学生信息
     */
    private void modifyStudentInfo() {
        System.out.println("以下是所有的学生信息");
        List<Student> students = stuControl.queryAll();
        for(Student item : students){
            System.out.println("-->" + item);
        }
        System.out.println("请指定学号修改，修改时请输入用户的所有信息");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] split = s.split(" ", 5);
        String sno = split[0];
        String name = split[1];
        int age = Integer.parseInt(split[2]);
        String gender = split[3];
        String phone = split[4];
        boolean b = stuControl.modifyStu(new Student(sno, name, age, gender, phone));
        if (b){
            System.out.println("修改成功");
            changeStudent();
        }else {
            System.out.println("修改失败");
            modifyStudentInfo();
        }
    }

    /**
     * 注册用户
     */
    private void registerUser() {
        System.out.println("请输入用户名和密码， 用空格隔开 例如：张三 123456");
        Scanner scanner = new Scanner(System.in);
        String userInfo = scanner.nextLine();
        String[] split = userInfo.split(" ", 2);
        String name = split[0];
        String psd = split[1];
        // 对用户密码加密
        psd = MD5Util.md5EnCode(psd);
        int end = userControl.registerUser(new User(name, psd));
        if (end == -2){
            System.out.println("该用户名已被注册，请修改后再次注册");
            registerUser();
        }else if (end == 0){
            System.out.println("注册失败，请重新注册");
            registerUser();
        }else if (end > 0){
            System.out.println("恭喜，注册成功，请登录");
            login();
        }else {
            System.out.println("程序异常");
        }
    }

}
